/////////////////////////////////////////////////////////////////////////////
// Name:        ax_summaryviewpanel.cpp
// Purpose:     
// Author:      Julian Smart
// Modified by: 
// Created:     27/03/2009 11:59:49
// RCS-ID:      
// Copyright:   
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wx.h"

////@begin includes
////@end includes

#include "ax/ax_summaryviewpanel.h"
#include "ax/ax_ui_adaptation_standard.h"

////@begin XPM images
////@end XPM images


/*
 * AxSummaryViewPanel type definition
 */

IMPLEMENT_DYNAMIC_CLASS( AxSummaryViewPanel, wxPanel )


/*
 * AxSummaryViewPanel event table definition
 */

BEGIN_EVENT_TABLE( AxSummaryViewPanel, wxPanel )
    EVT_SYS_COLOUR_CHANGED(AxSummaryViewPanel::OnSysColourChanged)

////@begin AxSummaryViewPanel event table entries
    EVT_BUTTON( ID_TEXT_SPEAK_ALL, AxSummaryViewPanel::OnTextSpeakAllClick )

    EVT_BUTTON( ID_TEXT_SPEAK_FROM_CURSOR, AxSummaryViewPanel::OnTextSpeakFromCursorClick )

    EVT_BUTTON( ID_TEXT_STOP, AxSummaryViewPanel::OnTextStopClick )
    EVT_UPDATE_UI( ID_TEXT_STOP, AxSummaryViewPanel::OnTextStopUpdate )

    EVT_BUTTON( ID_TEXT_PAUSE, AxSummaryViewPanel::OnTextPauseClick )
    EVT_UPDATE_UI( ID_TEXT_PAUSE, AxSummaryViewPanel::OnTextPauseUpdate )

    EVT_BUTTON( ID_TEXT_BACK, AxSummaryViewPanel::OnTextBackClick )

    EVT_BUTTON( ID_TEXT_FORWARD, AxSummaryViewPanel::OnTextForwardClick )

////@end AxSummaryViewPanel event table entries

END_EVENT_TABLE()


/*
 * AxSummaryViewPanel constructors
 */

AxSummaryViewPanel::AxSummaryViewPanel()
{
    Init();
}

AxSummaryViewPanel::AxSummaryViewPanel( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, pos, size, style);
}


/*
 * AxSummaryViewPanel creator
 */

bool AxSummaryViewPanel::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
{
////@begin AxSummaryViewPanel creation
    wxPanel::Create( parent, id, pos, size, style );

    CreateControls();
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    Centre();
////@end AxSummaryViewPanel creation

    return true;
}


/*
 * AxSummaryViewPanel destructor
 */

AxSummaryViewPanel::~AxSummaryViewPanel()
{
////@begin AxSummaryViewPanel destruction
////@end AxSummaryViewPanel destruction
}


/*
 * Member initialisation
 */

void AxSummaryViewPanel::Init()
{
////@begin AxSummaryViewPanel member initialisation
    m_parentSizer = NULL;
    m_textCtrl = NULL;
    m_controlSizer = NULL;
    m_speakAllCtrl = NULL;
    m_speakFromCursorCtrl = NULL;
    m_stopCtrl = NULL;
    m_backCtrl = NULL;
    m_forwardCtrl = NULL;
////@end AxSummaryViewPanel member initialisation
}


/*
 * Control creation for AxSummaryViewPanel
 */

void AxSummaryViewPanel::CreateControls()
{    
////@begin AxSummaryViewPanel content construction
    AxSummaryViewPanel* itemPanel1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemPanel1->SetSizer(itemBoxSizer2);

    m_parentSizer = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer2->Add(m_parentSizer, 1, wxGROW|wxALL, 5);

    wxStaticBitmap* itemStaticBitmap4 = new wxStaticBitmap( itemPanel1, wxID_STATIC, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0 );
    itemStaticBitmap4->Show(false);
    m_parentSizer->Add(itemStaticBitmap4, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    m_textCtrl = new AxTextCtrl( itemPanel1, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH2 );
    m_parentSizer->Add(m_textCtrl, 1, wxGROW|wxALL, 5);

    m_controlSizer = new wxBoxSizer(wxHORIZONTAL);
    m_parentSizer->Add(m_controlSizer, 0, wxALIGN_CENTER_HORIZONTAL, 5);

    m_speakAllCtrl = new wxButton( itemPanel1, ID_TEXT_SPEAK_ALL, _("Speak &All"), wxDefaultPosition, wxDefaultSize, 0 );
    m_speakAllCtrl->SetHelpText(_("Speaks all of the text."));
    m_controlSizer->Add(m_speakAllCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_speakFromCursorCtrl = new wxButton( itemPanel1, ID_TEXT_SPEAK_FROM_CURSOR, _("Speak from &Cursor"), wxDefaultPosition, wxDefaultSize, 0 );
    m_speakFromCursorCtrl->SetHelpText(_("Speaks only from the cursor position."));
    m_controlSizer->Add(m_speakFromCursorCtrl, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxTOP|wxBOTTOM, 5);

    m_stopCtrl = new wxButton( itemPanel1, ID_TEXT_STOP, _("S&top"), wxDefaultPosition, wxDefaultSize, 0 );
    m_stopCtrl->SetHelpText(_("Stops speaking."));
    m_controlSizer->Add(m_stopCtrl, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxTOP|wxBOTTOM, 5);

    wxButton* itemButton10 = new wxButton( itemPanel1, ID_TEXT_PAUSE, _("Paus&e"), wxDefaultPosition, wxDefaultSize, 0 );
    itemButton10->SetHelpText(_("Pauses speaking."));
    m_controlSizer->Add(itemButton10, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxTOP|wxBOTTOM, 5);

    m_backCtrl = new wxButton( itemPanel1, ID_TEXT_BACK, _("<<"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
    m_backCtrl->SetHelpText(_("Reads the previous sentence."));
    m_controlSizer->Add(m_backCtrl, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxTOP|wxBOTTOM, 5);

    m_forwardCtrl = new wxButton( itemPanel1, ID_TEXT_FORWARD, _(">>"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
    m_forwardCtrl->SetHelpText(_("Reads the next sentence."));
    m_controlSizer->Add(m_forwardCtrl, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxTOP|wxBOTTOM, 5);

////@end AxSummaryViewPanel content construction

    AxSelfVoicing::AddAdapter(m_forwardCtrl, new AxButtonAdapter(_("Next sentence")));
    AxSelfVoicing::AddAdapter(m_backCtrl, new AxButtonAdapter(_("Previous sentence")));

    m_adapter.Adapt(this);
}


/*
 * Should we show tooltips?
 */

bool AxSummaryViewPanel::ShowToolTips()
{
    return true;
}

/*
 * Get bitmap resources
 */

wxBitmap AxSummaryViewPanel::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin AxSummaryViewPanel bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end AxSummaryViewPanel bitmap retrieval
}

/*
 * Get icon resources
 */

wxIcon AxSummaryViewPanel::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin AxSummaryViewPanel icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end AxSummaryViewPanel icon retrieval
}


/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_SUMMARY_SPEAK
 */

void AxSummaryViewPanel::OnTextSpeakAllClick( wxCommandEvent& WXUNUSED(event) )
{
    m_textCtrl->GetTextEventHandler()->ReadFromStart();
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_TEXT_SPEAK_FROM_CURSOR
 */

void AxSummaryViewPanel::OnTextSpeakFromCursorClick( wxCommandEvent& WXUNUSED(event) )
{
    m_textCtrl->GetTextEventHandler()->ReadFromCurrentPosition();
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_TEXT_STOP
 */

void AxSummaryViewPanel::OnTextStopClick( wxCommandEvent& WXUNUSED(event) )
{
    m_textCtrl->GetTextEventHandler()->StopSpeech(AxSpeechContentChannel);
}


/*!
 * wxEVT_UPDATE_UI event handler for ID_TEXT_STOP
 */

void AxSummaryViewPanel::OnTextStopUpdate( wxUpdateUIEvent& event )
{
    event.Enable(m_textCtrl->GetTextEventHandler()->CanStop(AxSpeechContentChannel));
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_TEXT_PAUSE
 */

void AxSummaryViewPanel::OnTextPauseClick( wxCommandEvent& WXUNUSED(event) )
{
    m_textCtrl->GetTextEventHandler()->PauseOrResumeSpeech(AxSpeechContentChannel);
}


/*!
 * wxEVT_UPDATE_UI event handler for ID_TEXT_PAUSE
 */

void AxSummaryViewPanel::OnTextPauseUpdate( wxUpdateUIEvent& event )
{
    if (m_textCtrl->GetTextEventHandler()->CanResume(AxSpeechContentChannel))
        event.SetText(_("R&esume"));
    else
        event.SetText(_("Paus&e"));

    event.Enable(m_textCtrl->GetTextEventHandler()->CanPause(AxSpeechContentChannel) || m_textCtrl->GetTextEventHandler()->CanResume(AxSpeechContentChannel));
}

/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_TEXT_BACK
 */

void AxSummaryViewPanel::OnTextBackClick( wxCommandEvent& WXUNUSED(event) )
{
    m_textCtrl->GetTextEventHandler()->ReadPreviousSentence();
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_TEXT_FORWARD
 */

void AxSummaryViewPanel::OnTextForwardClick( wxCommandEvent& WXUNUSED(event) )
{
    m_textCtrl->GetTextEventHandler()->ReadNextSentence();
}

// Layout the container in response to possible size changes
void AxSummaryViewPanel::OnSysColourChanged(wxSysColourChangedEvent& event)
{
    Layout();

    event.Skip();
}
